package com.frank.loadinglibrary.common.element;

import com.frank.loadinglibrary.base.baseanimations.BaseAnimationController;
import com.frank.loadinglibrary.base.baseelement.BaseElement;
import com.frank.loadinglibrary.base.baseelement.IComponentCallback;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;

import java.util.ArrayList;

public class CircularRingElement extends BaseElement implements BaseAnimationController.IAnimationController<AnimatorValue> {

    private Paint mPaint;
    private Paint mPaintPro;

    RectFloat rectF = new RectFloat();
    CircularRingParams params;

    private CircularRingElement() {

    }

    private CircularRingElement(CircularRingParams params, IComponentCallback call) {
        super();
        this.params = params;
        this.callback = call;
    }

    @Override
    public void initPaint() {
        super.initPaint();
        if (mPaint == null) {
            mPaint = new Paint();
        }
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(params.mRingWidth);
        if (mPaintPro == null) {
            mPaintPro = new Paint();
        }
        mPaintPro.setAntiAlias(true);
        mPaintPro.setStyle(Paint.Style.STROKE_STYLE);
        mPaintPro.setColor(new Color(Color.argb(100, 255, 255, 255)));
        mPaintPro.setStrokeWidth(params.mRingWidth);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        float width = getWidth() <= getHeight() ? getWidth() : getHeight();
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, width / 2 - params.mPadding, mPaintPro);
        rectF = new RectFloat(getWidth() / 2 - width / 2 + params.mPadding, getHeight() / 2 - width / 2 + params.mPadding, getWidth() / 2 + width / 2 - params.mPadding, getHeight() / 2 + width / 2 - params.mPadding);
        //第四个参数是否显示半径
        canvas.drawArc(rectF, new Arc(params.startAngle, params.sweepAngle, false), mPaint);
    }

    @Override
    public ArrayList<AnimatorValue> onCreateAnimators() {
        ArrayList<AnimatorValue> animators = new ArrayList<>();
        AnimatorValue rorateAnim = new AnimatorValue();
        rorateAnim.setDuration(params.durtion);
        rorateAnim.setLoopedCount(-1);
        rorateAnim.setValueUpdateListener((animatorValue, v) -> {
            //设定 0.3 - 1之间缩放
            params.startAngle = 360 * v;
            if (callback != null) {
                callback.reflushComponent();
            }
        });
        animators.add(rorateAnim);
        return animators;
    }

    @Override
    public void onAnimationReset() {
        //do nothing because donot need to reset animation params
    }

    @Override
    public BaseAnimationController.IAnimationController<AnimatorValue> initAnimationController() {
        return this;
    }

    /**
     * 构建者
     */
    public static class CircularRingBuild {
        //默认时长基数
        private static final int BASE_DURTION_TIME = 1000;
        CircularRingParams params;
        IComponentCallback call;

        public CircularRingBuild(IComponentCallback c) {
            call = c;
            params = new CircularRingParams();
            setDurtion(BASE_DURTION_TIME);
        }


        public CircularRingBuild setDurtion(int dur) {
            params.durtion = dur;
            return this;
        }

        public CircularRingBuild setRingWidth(float rw) {
            params.mRingWidth = rw;
            return this;
        }

        public CircularRingBuild setPadding(float padding) {
            params.mPadding = padding;
            return this;
        }

        public CircularRingBuild setStartAngle(float angle) {
            params.startAngle = angle;
            return this;
        }

        public CircularRingBuild setSweepAngle(float angle) {
            params.sweepAngle = angle;
            return this;
        }

        public CircularRingElement build() {
            return new CircularRingElement(params, call);
        }
    }

    /***
     * 可配置参数类
     */
    public static class CircularRingParams {
        private float mRingWidth = 8;
        private float mPadding = 5.0f;
        private float startAngle = 0f;
        private float sweepAngle = 100;
        private int durtion = 1000;

    }
}
